#pragma once

#include "../engine/Singleton.h"
#include "../engine/ProgramState.h"
#include "../engine/input.h"

namespace Putin{
/**
 * The primary state machine that will control the top view 
 * operations of the game engine states. Moving from menu to 
 * game mode, etc.
 */
class EngineController : public Singleton<EngineController>
{
public:
	EngineController(void);
	virtual ~EngineController(void);
	
	/*!
	 * \brief Initialize the Game Engine one-time init. Subsystems are inited now as well.
	 * 
	 * \returns true on success
	 */
	virtual bool init();

	/*!
	 * \brief
	 * Terminate the engine system. 
	 * This is responsible to terminate the current Program
	 * as well as system wide subsystems (render engine etc)
	 * 
	 * \returns true on success
	 * \remarks  Called by the destructor, if invoked.
	 */
	virtual bool terminate();


	/*!
	 * \brief Start the Engine Controller main loop
	 * \returns Status of execution
	 * Runs the current set program
	 */
	virtual bool start();

	/*!
	 * \brief Preload a program state, such as BattleControl or Lobby
	 * 
	 * This calls the loadResources() on the program state, and sets this state as the next to start
	 * \param prog The state to load
	 */
	virtual bool preloadState(ProgramState* prog);

	/*!
	 * \brief Change from one state to the next one
	 * 
	 * When switching from one state, the previous state is terminated
	 * calling "terminate()" function and then the nextState is initialized.
	 * However, the nextState is presumed to have the resources loaded with
	 * loadResources() using for instance changeState() before.
	 */
	virtual bool changeState();
	virtual bool update();

	virtual void quit();

protected:

	virtual bool render();

	ProgramState*	mNextProgram;
	ProgramState*	mProgram;

	bool mInited;
	bool mContinue;

	InputManager*	mInput;


private: 
	double elapsedSinceSoundUpdate;
	void keyEvent(Putin::InputArg ia);
	void mouseEvent(Putin::InputArg ia);
};

} // end namespace Putin